﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;
using Mengsk.ProxyDelivery.Domain;

namespace Mengsk.ProxyDelivery.App.UI.Seller
{
    /// <summary>
    /// SellerLoginUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class SellerLoginUserControl : UserControl, IPlugin
    {
        private LoginHistoryService loginHistoryService = ServiceContainer.GetService<LoginHistoryService>();

        public SellerLoginUserControl()
        {
            InitializeComponent();
        }

        public string Title
        {
            get { return "登录记录"; }
        }

        public Control UIControl
        {
            get { return this; }
        }


        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.pageBar.Parameters.Clear();
                int sellerId = 0;
                int.TryParse(this.tbSellerId.Text.Trim(), out sellerId);
                DateTime? start = this.dpStart.SelectedDate;
                DateTime? end = this.dpEnd.SelectedDate;
                this.pageBar.Parameters.Add("sellerId", sellerId);
                this.pageBar.Parameters.Add("ipAddress", this.tbIP.Text.Trim());
                this.pageBar.Parameters.Add("result", this.cbbResults.SelectedIndex - 1);
                this.pageBar.Parameters.Add("startTime", start == null ? DateTime.MinValue : start.Value.Date);
                this.pageBar.Parameters.Add("endTime", end == null ? DateTime.MinValue : end.Value.Date.AddDays(1));
                this.pageBar.StartPage();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void pageBar_PageChanging(object sender, PageBar.PageChangeEventArgs e)
        {
            var data = this.loginHistoryService.GetByAllOfSeller(e.GetParameter<int>("sellerId"), LoginHistorySourceTypes.None, e.GetParameter<string>("ipAddress"), e.GetParameter<int>("result"), e.GetParameter<DateTime>("startTime"), e.GetParameter<DateTime>("endTime"), e.CurrentPage - 1, e.PageSize);
            this.dgvLoginHistories.ItemsSource = data.Datas;
            this.pageBar.Total = data.Total;
            this.pageBar.CurrentCount = data.Datas.Count;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.loginHistoryService.Delete(this.dgvLoginHistories.SelectedItems.OfType<LoginHistory>().Select(obj => obj.Id).ToArray());
                MessageBox.Show("删除成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    }
}
